package com.weds.frame.core.config.swagger;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2023/3/13 14:34
 */
@Data
@Component
@ConfigurationProperties(prefix = "swagger")
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger.basePackage}")
    private String basePackage;
    @Value("${swagger.projectName}")
    private String projectName;
    @Value("${swagger.version}")
    private String version;
    @Value("${swagger.active}")
    private Boolean active;

//    @Value("${jwt.filter}")
//    private String filter;
//
//    private static List<Pattern> patterns = new ArrayList();
//
//    @PostConstruct
//    public void init(){
//        if(StringUtils.isNotEmpty(filter)){
//            String[] filters = filter.split(",");
//            String[] var3 = filters;
//            int var4 = filters.length;
//            for (int var5 = 0; var5 < var4; ++var5) {
//                String filter = var3[var5];
//                patterns.add(Pattern.compile(filter));
//            }
//        }
//    }


    @Bean
    public Docket createRestApi(Environment environment) {
        ParameterBuilder ticketPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        ticketPar.name("token").description("令牌").modelRef(new ModelRef("String")).parameterType("header")
                .required(true).build();
        pars.add(ticketPar.build());

        Profiles profile = Profiles.of("pro");
        Boolean show = environment.acceptsProfiles(profile);

        return new Docket(DocumentationType.SWAGGER_2)
                .globalResponseMessage(RequestMethod.GET, getGlobalStatusCode())
                .globalResponseMessage(RequestMethod.POST, getGlobalStatusCode())
                .globalResponseMessage(RequestMethod.PUT, getGlobalStatusCode())
                .globalResponseMessage(RequestMethod.DELETE, getGlobalStatusCode())
                .apiInfo(apiInfo())
                .enable(active == null ? true : active)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档，忽略哪些请求
                .build().globalOperationParameters(pars);
    }

//    @Bean
//    public Docket createRestApi1(Environment environment) {
//        Profiles profile = Profiles.of("pro");
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                .enable(active == null ? true : active)
//                .select()
//                .apis(RequestHandlerSelectors.basePackage(basePackage))
//                .paths(regex) // 可以根据url路径设置哪些请求加入文档，忽略哪些请求
//                .build();
//    }

    private ApiInfo apiInfo() {
        String title = StringUtils.isEmpty(projectName) ? "接口文档" : (projectName + "接口文档");
        return new ApiInfoBuilder()
                .title(title)
                .description(title)
                .version(StringUtils.isNotEmpty(version) ? version : "1.0")
                .termsOfServiceUrl("http://www.baidu.com")
                .build();
    }

    /**
     * 定义全局状态码
     * @return
     */
    public List<ResponseMessage> getGlobalStatusCode(){
        List<ResponseMessage> responseMessageList = new ArrayList<>();
        ResponseMessage successResponse1 = new ResponseMessageBuilder().code(0).message("成功").responseModel(new ModelRef("数据")).build();
        ResponseMessage successResponse2 = new ResponseMessageBuilder().code(600).message("成功").responseModel(new ModelRef("数据")).build();
        ResponseMessage successResponse3 = new ResponseMessageBuilder().code(601).message("失败: 非0,600都是失败").responseModel(new ModelRef("数据")).build();
        responseMessageList.add(successResponse1);
        responseMessageList.add(successResponse2);
        responseMessageList.add(successResponse3);

        return  responseMessageList;
    }

}
